﻿using SuporeSliplampApp.Control;
using SuporeSliplampApp.Models;
using SuporeSliplampApp.Service;
using SuporeSliplampApp.Util;
using SuporeSliplampApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FellowOakDicom;
using FellowOakDicom.Imaging;
using FellowOakDicom.IO.Writer;
using FellowOakDicom.Network.Client;
using FellowOakDicom.Network;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// ImpDCM.xaml 的交互逻辑
    /// </summary>
    public partial class ImpDCM : Window
    {
        List<PatientModel> patientList = new List<PatientModel>();
        List<PatientModel> patientList2 = new List<PatientModel>();
        DicomPatientService dicomPatientService = new DicomPatientService();
        OpenPatientsModel openPatientsModel = new OpenPatientsModel();

        public ImpDCM()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var model = this.grid.SelectedItem as PatientModel;
            MainViewModel.GetInstance().patientModel = model;
            util.isPatientSelected = true;
            HomeViewModel.GetInstance().NavPage("SuporeSliplampApp.Views.CameraView");
            this.Close();
        }


        private async void InitData()
        {
            patientList = await GetPatients();
            openPatientsModel.RecordCount = patientList.Count;
            await Load(1, openPatientsModel.PageSize);
        }


        private async Task<List<PatientModel>> GetPatients()
        {
            
            var dicomClient = DicomConnectionManager.Instance.GetClient();
            if (dicomClient != null)
            {
                return await dicomPatientService.GetPatientsFromDicomClient(dicomClient);
            }
            else
            {
                if (Common.languageCode == "zh-CN")
                {
                    MessageBox.Show("未连接到DICOM服务器！");
                }
                else
                {
                    MessageBox.Show("DICOM Server Disconnected!");
                }
                //MessageBox.Show("未连接到DICOM服务器。");
                return new List<PatientModel>();
            }
        }

        public Task Load(int page, int pageSize)
        {
            return Task.Run(() =>
            {
                App.Current.Dispatcher.Invoke(() =>
                {
                    var list = patientList.Skip((page - 1) * pageSize).Take(pageSize).ToList();
                    patientList2 = list;
                    grid.ItemsSource = patientList2;
                });
            });
        }

        private void Paginator_Loaded(object sender, RoutedEventArgs e)
        {
            InitData();
        }

        private void Paginator_PageChanged(object arg1, Control.PageEventArgs arg2)
        {
            Paginator paginator = arg1 as Paginator;
            Load(paginator.Page, paginator.PageSize);
        }

    }
}
